package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Information;
import cn.wolfcode.trip.base.domain.Praise;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.InformationMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private InformationMapper informationMapper;

    @Override
    public void saveOrUpdate(Travel travel) {
        Date date = new Date();
        if (travel.getId() == null) {
            travel.setCreateTime(date);
            travel.setLastUpdateTime(date);
            travelMapper.insert(travel);
            travel.getTravelContent().setId(travel.getId());
            travelContentMapper.insert(travel.getTravelContent());
        } else {
            travel.setLastUpdateTime(date);
            travelMapper.updateByPrimaryKey(travel);
            travel.getTravelContent().setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travel.getTravelContent());
        }

    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Object getByUserId(Long id) {

        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryForWait(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryForWaitAndRelease(qo);
        return new PageInfo(list);
    }

    @Override
    public void changeState(Travel travel) {
        travel.setReleaseTime(new Date());
      travelMapper.updateState(travel);
    }

    @Override
    public Travel getContent(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryForRelease(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryForWaitAndRelease(qo);
        for (Travel travel : list) {
           Integer replyNum = travelMapper.selectReplyNum(travel.getId());
           travel.setReplyNum(replyNum);
        }
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryByStrategyId(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.queryByStrategyId(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Travel> queryTravel(TravelQueryObject qo) {
        return travelMapper.queryForList(qo);
    }

    @Override
    public void saveRelation(Long strategyId, Long travelId) {
        travelMapper.insertRelation(strategyId,travelId);
    }

    @Override
    public void delete(Long strategyId, Long travelId) {
        travelMapper.deleteRelation(strategyId,travelId);
    }

    @Override
    public Boolean queryIsRelation(Long travelId, Integer state) {
        User user = UserContext.getUserAttribute();
        String s = travelMapper.selectIsRelation(travelId, user.getId(), state);
        if (s == null) {
            return false;
        }
        return true;
    }


    @Override
    public void insertRelation(Praise obj) {
        User user = new User();
        User author = new User();
        Date date = new Date();
        travelMapper.insertUserRelation(obj);
        travelMapper.updateAddNums(obj.getTravelId());
        //发送消息
        System.out.println(obj.getTravelId());
        informationMapper.insertRelation(obj.getTravelId(),3);
        user.setId(obj.getUserId());
        author.setId(obj.getAuthorId());
        informationMapper.insert(new Information(user,author,3,date));
    }

    @Override
    public void deleteTravelRelation(Long travelId, Long userId, Integer state) {
        travelMapper.deleteTravelRelation(travelId,userId,state);
        travelMapper.updateSubNums(travelId);
    }

    @Override
    public Map<String, Object> queryNumsOfTravelContent(Integer state,Long travelId) {
        Integer praiseCount=travelMapper.selectNumsOfTravelContent(state,travelId);
        Map<String,Object> map = new HashMap<>();
        map.put("praiseCount",praiseCount);
        return map;
    }


    //查询当前用户对游记是否评论
    @Override
    public List queryRows(Long authorId,Long userId, Integer state) {
        return travelMapper.queryTravelIdRows(authorId,userId,state);
    }

    @Override
    public Integer queryTravelContentNum(Long authorId) {

        return travelMapper.selectTravelContentNum(authorId);

    }

    @Override
    public void updateBrowseNum(Long travelId) {
        travelMapper.updateBrowseNum(travelId);
    }
}
